<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/4/1
 * Time: 13:55
 */

namespace app\controller\admin;
use app\model\goods AS model;
class Goods extends Base
{
    public function list(){
        $post = input('post.');
        $where = [];
        $model = new model();
        $goods = $model
            ->where($where)
            ->order('id DESC')
            ->paginate(['page' => $this->page,'list_rows' => $this->size]);
        return $this->view('/admin/goods/list',['list' => $goods]);
    }

    public function save(){
        $xlsx_data = input('xlsx_data');
        if (!empty($xlsx_data)){

        }else{
            $create = [];
            $post = input('post.');
            if(empty($post['id'])) {
                foreach ($post AS $key => $value) {
                    $create[strtolower($key)] = $value;
                }
                model::create($create);
            }else{
                model::where('id',$post['id'])
                    ->save([
                        $post['key'] => $post['value']
                    ]);
                return $this->msg();
            }
        }
        return $this->msg();
    }

    public function delete(){
        $id = input('id');
        $ids = input('ids');
        if (!empty($ids)){
            (new model())->where('id','IN',$ids)->delete();
        }else{
            (new model())->where('id',$id)->delete();
        }
        return $this->msg();
    }
    
    public function material_list(){
        $post = input('post.');
        $where = [];
        !empty($post['name']) && $where[] = ['name','like','%'.$post['name'].'%'];
        !empty($post['quality']) && $where[] = ['quality','like','%'.$post['quality'].'%'];
        !empty($post['attr']) && $where[] = ['attr','like','%'.$post['attr'].'%'];
        !empty($post['supplier']) && $where[] = ['supplier','like','%'.$post['supplier'].'%'];
        $model = new material();
        $goods = $model
            ->is_new(true)
            ->where($where)
            ->paginate(['page' => $this->page,'list_rows' => $this->size]);
        return $this->view('/admin/goods/material/list',['list' => $goods]);
    }
    
    public function material_save(){
        $supplier = input('supplier');
        $xlsx_data = input('xlsx_data');
        $message = [];
        if (!empty($xlsx_data)){
            $attr = explode("\n",$xlsx_data);
            if (empty($attr[0]) || str_replace(',','',$attr[0]) != '材质规格价格密度(g/cm³)')
                return $this->fail('文件内容格式错误1');
            $install = [];
            unset($attr[0]);
            array_shift($attr);
            foreach ($attr AS $key => $value){
                $array = explode(",",$value);
                if (empty($array[0]))continue;
                $attr_list = explode('/',$array[1]);
                if (!empty($attr_list)){
                    foreach($attr_list AS $index => $item){
                        $install[] = [
                            'name' => $array[0],
                            'attr' => $item ?? '',
                            'price' => $array[2] ?? 0,
                            'density' => $array[3] ?? 0,
                            'supplier' => $supplier,
                            'create_time' => date('Y-m-d H:i:s'),
                            'update_time' => date('Y-m-d H:i:s')
                        ];
                    }
                }else{
                    $install[] = [
                        'name' => $array[0],
                        'attr' => $array[1] ?? '',
                        'price' => $array[2] ?? 0,
                        'density' => $array[3] ?? 0,
                        'supplier' => $supplier,
                        'create_time' => date('Y-m-d H:i:s'),
                        'update_time' => date('Y-m-d H:i:s')
                    ];
                }
            }
        }
        if(!empty($install))(new material())->all_install($install);
        return $this->msg($message);
    }

}